/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.domain;


import han.bifang.business.domain.ServiceBusManager;
import han.bifang.common.entity.BusBindingWrapper;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaBusBinding;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaNormalJms;
import han.bifang.common.entity.SoaServiceBus;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @Author Kent
 */
public class ModifyBusAction implements BifangGWTAction {

	@Override
	public ResponseObject execute(RequestObject req,HttpServletRequest request,HttpServletResponse response) throws BifangException {
		ResponseObject res = new ResponseObject();
		try {
			HashMap h = (HashMap) req.getRequest();
			ServiceBus sc = (ServiceBus) h.get("bus");
			ArrayList bindings = (ArrayList) h.get("bindings");

			SoaServiceBus ssc = new SoaServiceBus();
			BeanUtils.copyProperties(ssc, sc);

			ArrayList cab = new ArrayList();
			if (bindings != null) {
				for (int i = 0, j = bindings.size(); i < j; i++) {
					BusBindingWrapper cbw = new BusBindingWrapper();
					BusBinding cb = (BusBinding) bindings.get(i);
					SoaBusBinding scb = new SoaBusBinding();
					BeanUtils.copyProperties(scb, cb);
					cbw.setBusBinding(scb);
					if (cb.getType().equals(EntityConstants.BINDING_HTTP)) {

						HttpBinding hb = (HttpBinding) cb.getBinding();
						SoaHttpBinding shb = new SoaHttpBinding();
						shb.setUrl(hb.getUrl());
						cbw.setHttpBinding(shb);
					} else if (cb.getType()
							.equals(EntityConstants.BINDING_SOAP)) {
						SoapBinding hb = (SoapBinding) cb
								.getBinding();
						SoaSoapBinding shb = new SoaSoapBinding();
						BeanUtils.copyProperties(shb, hb);
						cbw.setSoapBinding(shb);
					}else if (cb.getType()
							.equals(EntityConstants.BINDING_WEB_SERVICES)) {
						SoapBinding hb = (SoapBinding) cb
								.getBinding();
						SoaSoapBinding shb = new SoaSoapBinding();
						BeanUtils.copyProperties(shb, hb);
						cbw.setSoapBinding(shb);
					}  
					else if (cb.getType().equals(EntityConstants.BINDING_JMS)) {
						JmsBindingWrapper hb = (JmsBindingWrapper)cb.getBinding();
						SoaJmsBinding shb = new SoaJmsBinding();
						BeanUtils.copyProperties(shb, hb.getJmsBinding());
						SoaNormalJms jj = new SoaNormalJms();
						BeanUtils.copyProperties(jj, hb.getInfo());
						
						SoaJmsBindingWrapper w = new SoaJmsBindingWrapper();
						w.setInfo(jj);
						w.setJmsBinding(shb);
						cbw.setJmsBinding(w);
					}
					cab.add(cbw);

				}
			}
			ServiceBusManager.modifyServiceBus(ssc,
					cab);
			res.setCode("0");
			//res.setResponse(ret);

		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;

	}

}
